let questionDetailApp = new Vue({
    el: '#questionDetailApp',
    data: {
        question: {
            title: '测试问题的标题',
            content: '测试问题的正文',
            hits: 629,
            userNickName: '奥特曼',
            gmtCreateText: '12分钟前'
        },
        answers :[
            {
                id: 3,
                content: '模拟答案-3',
                userId: 39,
                userNickName: '怪兽-3',
                isAccepted: 0,
                gmtCreateText: '3分钟前',
                comments: [
                    { userNIckName: '评论员1号',content: '模拟评论1'},
                    { userNIckName: '评论员2号',content: '模拟评论2'},
                    { userNIckName: '评论员3号',content: '模拟评论3'},
                    { userNIckName: '评论员4号',content: '模拟评论4'}
                ]
            },
            {
                id: 2,
                content: '模拟答案-2',
                userId: 29,
                userNickName: '怪兽-2',
                isAccepted: 0,
                gmtCreateText: '12分钟前',
                comments: []
            },
            {
                id: 1,
                content: '模拟答案',
                userId: 19,
                userNIckName: '怪兽-1',
                isAccepted: 0,
                gmtCreateText: '18分钟前',
                comments: []
            }
        ]
    },
    methods: {
        loadQuestionDetail: function(){
            //alert(‘准备加载问题详情....’);
            //isNaN ->is Not a Number
            let id = location.search.substring(1);
            if(id == "" || isNaN(id)){
                alert('错误:缺少必要的参数!');
                location.href = "/";
                return ;
            }
            $.ajax({
                url: '/portal/question/'+id,
                success: function(r){
                    if(r.state ==2000){
                        let question = r.data;
                        question.gmtCreateText = getTimeText(question.gmtCreate);
                        questionDetailApp.question = question;
                    }else {
                        alert(r.message);
                        location.href = "/";
                    }
                }
            });
        },
        postAnswer: function(){
            // alert('准备发布答案....');
            $.ajax({
                url: '/portal/answer/post',
                data: {
                    questionId:location.search.substring(1),
                    content: $('#summernote').val()
                },
                type:'post',
                success: function (r){
                    if(r.state == 2000){
                        alert("发表答案成功!");
                        questionDetailApp.loadAnswer();
                        location.href='#answers';
                        $('#summernote').summernote('reset');
                    }else{
                        alert(r.message);
                        location.href = "/";
                    }
                }
            });
        },
        loadAnswer: function(){
            //alert(‘准备加载答案列表...’);
            $.ajax({
                url: '/portal/answer',
                data: 'questionId=' +location.search.substring(1),
                success: function(r){
                    let list = r.data;

                    for(let i = 0;i < list.length; i++){
                        list[i].gmtCreateText = getTimeText(list[i].gmtCreate);
                    }
                    questionDetailApp.answers = list ;
                }
            });
        },
        postComment: function(answerId){
            //alert('准备发表评论....');
            $.ajax({
                url: '/portal/comment/post',
                data: {
                    answerId: answerId,
                    content: $('#commentContent' + answerId).val()
                },
                type: 'post',
                success: function(r){
                    if(r.state == 2000){
                        alert('发表评论成功!');
                        //待续
                    }else{
                        alert(r.message);
                        questionDetailApp.loadAnswer();
                    }
                }
            });
        },
        // loadComments: function(){
        // }
    },

    created: function(){
        this.loadQuestionDetail();
        this.loadAnswer();
        //this.loadComments();
    }
});